<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/16 11:23
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use app\common\service\CommonService;
use think\facade\View;

class Feedback extends Common
{
    public function index()
    {
        $param = input('param.');
        $keywords = input('param.keywords');
        $list_where = [];
        if (!empty($keywords)){
            $member_where = [];
            if (is_mobile($keywords)){
                $member_where['mobile'] = $keywords;
            }else{
                $member_where['account'] = $keywords;
            }
            $member_model = new \app\common\model\Member();
            $member_id = $member_model->getField($member_where, 'member_id');
            if (empty($member_id)){
                $list_where['member_id'] = 0;
            }else{
                $list_where['member_id'] = $member_id;
            }
        }
        // 查询时间
        $start_time = empty($param['start_time']) ? '' : str_replace('%2B', ' ', $param['start_time']);
        $end_time = empty($param['end_time']) ? '' : str_replace('%2B', ' ', $param['end_time']);
        if (!empty($start_time)){
            if (strtotime($start_time)){
                $beg = strtotime($start_time);
                if (!empty($end_time) && strtotime($end_time)){
                    $end = strtotime($end_time);
                }else{
                    $date = date('Y-m-d',NEW_TIME) . ' 23:59:59';
                    $end = strtotime($date);
                }
                $list_where['create_time'] = [['egt',$beg],['elt',$end]];
            }
        }
        $feedback_model = new \app\common\model\Feedback();
        $data = $feedback_model->feedbackLists($list_where, 10, 'create_time desc');

        View::assign([
            'keywords'=>$keywords,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'count'=>$data['count'],
            'list'=>$data['list'],
            'page'=>$data['page']
        ]);
        return View::fetch();
    }

    public function viewFeedback()
    {
        $feedback_id = input('param.feedback_id');
        if (empty($feedback_id)){
            $this->redirect('Feedback/index', ['p'=>$this->p]);
        }
        $feedback_model = new \app\common\model\Feedback();
        $data = $feedback_model->getFind($feedback_id);
        if (empty($data)){
            $this->redirect('Feedback/index', ['p'=>$this->p]);
        }
        $data['photo'] = empty($data['photo']) ? [] : explode(',', $data['photo']);
        $member_model = new \app\common\model\Member();
        $member_data = $member_model->getFind($data['member_id'], 'member_id,account,nickname');
        $data['account'] = $member_data['account'];
        $data['nickname'] = $member_data['nickname'];

        View::assign([
            'data'=>$data
        ]);
        return View::fetch();
    }

    public function replyFeedback()
    {
        $feedback_id = input('param.feedback_id',0,'intval');
        if (empty($feedback_id)){
            $this->returnError('操作失败');
        }
        if (request()->isPost())
        {
            $data = input('post.');
            if (empty($data['reply'])){
                $this->returnError('请输入回复内容');
            }
            $curr_time = NEW_TIME;
            $data['update_time'] = $curr_time;
            $data['admin_id'] = $this->adminId;
            $feedback_model = new \app\common\model\Feedback();
            $rows = $feedback_model->operation($data, false);
            if (empty($rows)){
                $this->returnError('操作失败');
            }
            if ($data['state'] == 1){
                $feedback_content = $feedback_model->getField($data['feedback_id'], 'content');
                $contents = $feedback_content . '；' . $data['reply'];
                // 发送消息
                $message_data = [
                    'target'=>1,
                    'type'=>1,
                    'title'=>'最新建议反馈结果',
                    'contents'=>$contents,
                    'create_time'=>$curr_time,
                    'send_time'=>$curr_time,
                    'target_id'=>$data['member_id'],
                    'is_read'=>1,
                    'is_send'=>1
                ];
                $message_model = new \app\common\model\Messages();
                $messages_id = $message_model->insert($message_data, false);
                if ($messages_id){
                    $member_mode = new \app\common\model\Member();
                    $push_alias = $member_mode->getField($data['member_id'], 'alias');
                    if (!empty($push_alias)){
                        $extras = [
                            'type'=>'system',
                            'contents'=>$message_data['contents'],
                            'ids'=>$messages_id
                        ];
                        $setting_model = new \app\common\model\Setting();
                        $setting_model->ruleExec($message_data['title'], $push_alias, $extras);
                    }
                }
            }
            $this->returnSuccess('回复成功', url('Feedback/index'), true);
        }
        View::assign('feedback_id', $feedback_id);
        return View::fetch();
    }

    public function auditFeedback()
    {
        if (CommonService::audit('status')){
            $this->returnSuccess('操作成功');
        }
        $this->returnError('操作失败');
    }

    public function deleteFeedback()
    {
        if (CommonService::delete()){
            $this->returnSuccess('删除成功');
        }
        $this->returnError('删除失败');
    }

}